<?php

namespace App\Http\Controllers\Admin;

use App\Models\BingChuang;
use App\Models\Bingqu;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class BingquController extends Controller
{
    protected $fields = [
        'name' => '',
        'leader' => '',
        'num' => 0,
    ];

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = Bingqu::orderBy('id','desc')->paginate(15);
        return view('admin.bingqu.index',['data'=>$data]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }

        return view('admin.bingqu.create',['data'=>$data]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $bingqu = new Bingqu();
        foreach (array_keys($this->fields) as $field) {
            $bingqu->$field = $request->get($field);
        }
        $bingqu->save();

        for ($i=0; $i<$request->input('num'); $i++){
            $bingchuang = new BingChuang();
            $bingchuang->bingqu_id = $bingqu->id;
            $bingchuang->code = $bingqu->name.'-'.($i+1);
            $bingchuang->save();
        }
        return redirect('/admin/bingqu')->withSuccess('添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $bingqu = Bingqu::find($id);
        if(!$bingqu){
            //Toastr::error('找不到该数据！');
            return redirect('/admin/bingqu')->withErrors('找不到该数据');
        }
        foreach (array_keys($this->fields) as $field) {
            $data[$field] = old($field, $bingqu->$field);
        }
        $data['id'] = (int)$id;

        return view('admin.bingqu.edit',['data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $bingqu = Bingqu::find((int)$id);
        if($request->input('num') < $bingqu->num){
            return redirect()->back()->withErrors('病床数不能小于原来数量');
        }
        $num_old = $bingqu->num;
        $add_num = $request->input('num') - $bingqu->num;
        foreach (array_keys($this->fields) as $field) {
            $bingqu->$field = trim($request->get($field));
        }
        $bingqu->save();
        for ($i=0; $i<$add_num; $i++){
            $bingchuang = new BingChuang();
            $bingchuang->bingqu_id = $bingqu->id;
            $bingchuang->code = $bingqu->name.'-'.($num_old+$i+1);
            $bingchuang->save();
        }

        //Toastr::success('编辑成功！');
        return redirect('/admin/bingqu')->withSuccess('编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    //public function destroy($id)
    //{
    //    $tag = Bingqu::find((int)$id);
    //    if ($tag) {
    //        $tag->delete();
    //    } else {
    //        Toastr::error('删除失败！');
    //        return redirect()->back();
    //    }
    //
    //    Toastr::success('删除成功！');
    //    return redirect()->back();
    //}
}
